/*
 * Copyright 2022-2023 the original author(https://github.com/wj596)
 * 
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */
package org.jsets.fastboot.flowable.controller;

import org.jsets.fastboot.model.BaseResp;
import org.jsets.fastboot.model.DataResp;
import org.jsets.fastboot.model.ListResp;
import org.jsets.fastboot.flowable.model.dto.FlowUser;
import org.jsets.fastboot.flowable.model.dto.FlowUserGroup;
import org.jsets.fastboot.flowable.model.dto.FlowUserGroupQuery;
import org.jsets.fastboot.flowable.model.dto.FlowUserQuery;
import org.jsets.fastboot.flowable.service.IFlowIdentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author wangjie (https://github.com/wj596)
 * @date 2021.06.25 11:23
 */
@Api(tags = "身份管理")
@RestController
@RequestMapping("/flow/identities")
public class FlowIdentityController {

	@Autowired
	private IFlowIdentityService service;

	@PostMapping(value="/groups", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("保存审批组")
	public BaseResp saveGroup(@RequestBody @Validated FlowUserGroup entity) {
		this.service.saveGroup(entity);
		return BaseResp.ok();
	}
	
	@ApiOperation("查询审批组")
	@GetMapping(value = "/groups", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiImplicitParams({
		 @ApiImplicitParam(name="page_no", value="当前页码", required=false, dataType = "Integer", dataTypeClass = Integer.class, paramType="query")
		,@ApiImplicitParam(name="page_size", value="每页条数", required=false, dataType = "Integer", dataTypeClass = Integer.class, paramType="query")
		,@ApiImplicitParam(name="name", value="组名称", required=false, dataType = "String", dataTypeClass = String.class, paramType="query")
	})
	public ListResp<FlowUserGroup> selectGroups(
			@RequestParam(name = "page_no", required = false) Integer pageNo,
			@RequestParam(name = "page_size", required = false) Integer pageSize,
			@RequestParam(name = "name", required = false) String name) {

		FlowUserGroupQuery criteria = new FlowUserGroupQuery()
				.setCurrent(pageNo)
				.setSize(pageSize)
				.setName(name);
		ListResp<FlowUserGroup> respond = ListResp.ok();
		if(criteria.isPageNecessary()) {
			Page<FlowUserGroup> result = this.service.selectFlowGroupPage(criteria);
			respond.setData(result.getRecords());
			respond.setTotalCount(result.getTotal());
			return respond;
		}
		respond.setData(this.service.selectFlowGroupList(criteria));
		return respond;
	}
	
	@DeleteMapping(value="/groups/{id}" ,produces = MediaType.APPLICATION_JSON_VALUE)
	public BaseResp deleteGroup(@PathVariable("id") String id) {
		this.service.deleteGroup(id);
		return BaseResp.ok();
	}
	
	@GetMapping(value="/groups/{id}" ,produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("获取审批组")
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="主键", required=true, dataType = "String", dataTypeClass = String.class, paramType="path")
	})
	public DataResp<FlowUserGroup> get(@PathVariable("id") String id) {
		DataResp<FlowUserGroup> respond = DataResp.ok();
		respond.setData(this.service.getGroupById(id));
		return respond;
	}
	
	@GetMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE)
	public ListResp<FlowUser> selectUsers(
			@RequestParam(name = "page_no", required = true) Integer pageNo,
			@RequestParam(name = "page_size", required = true) Integer pageSize,
			@RequestParam(name = "account", required = false) String account,
			@RequestParam(name = "name", required = false) String name) {

		FlowUserQuery criteria = new FlowUserQuery()
				.setCurrent(pageNo)
				.setSize(pageSize)
				.setAccount(account)
				.setName(name);
		ListResp<FlowUser> respond = ListResp.ok();
		Page<FlowUser> result = this.service.selectFlowUserPage(criteria);
		respond.setData(result.getRecords());
		respond.setTotalCount(result.getTotal());
		return respond;
	}
	
	@PostMapping(value="/users", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public BaseResp saveUser(@RequestBody @Validated FlowUser entity) {
		this.service.saveUser(entity);
		return BaseResp.ok();
	}

	@DeleteMapping(value="/users/{account}" ,produces = MediaType.APPLICATION_JSON_VALUE)
	public BaseResp deleteUser(@PathVariable("account") String account) {
		this.service.deleteUser(account);
		return BaseResp.ok();
	}
	
}